package com.example.practice.guidance;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.practice.R;
import com.example.practice.adapter.viewPagerAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private ImageView indicator;
    ImageView[] indicators;
    TextView TV_setPort;
    TextView TV_start;
    private LinearLayout indicatorGroup;
    ArrayList<ImageView> imageViews = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.viewPager);
        indicatorGroup = findViewById(R.id.indicatorGroup);
        TV_setPort = findViewById(R.id.setPort);
        TV_start = findViewById(R.id.start);
        initGuidance();
    }

    public void initGuidance(){
        ImageView img1 = new ImageView(this);
        img1.setBackgroundResource(R.drawable.yingdao1);
        imageViews.add(img1);
        ImageView img2 = new ImageView(this);
        img2.setBackgroundResource(R.drawable.yingdao2);
        imageViews.add(img2);
        ImageView img3 = new ImageView(this);
        img3.setBackgroundResource(R.drawable.yingdao3);
        imageViews.add(img3);
        ImageView img4 = new ImageView(this);
        img4.setBackgroundResource(R.drawable.yingdao4);
        imageViews.add(img4);
        ImageView img5 = new ImageView(this);
        img5.setBackgroundResource(R.drawable.yingdao5);
        imageViews.add(img5);
        //动态加载指示器
        indicators = new ImageView[imageViews.size()];
        for (int i=0;i<indicators.length;i++){
            indicator = new ImageView(this);
            indicator.setLayoutParams(new LinearLayout.LayoutParams(45,45));
            indicator.setPadding(5,5,5,5);
            indicator.setId(i);
            if (i==0){
                indicator.setBackgroundResource(R.drawable.white);
            }else{
                indicator.setBackgroundResource(R.drawable.gray);
            }
            indicators[i] = indicator;
            indicatorGroup.addView(indicators[i]);
        }
        //为指示器添加点击事件
        for (int i=0;i<imageViews.size();i++){
            int finalI1 = i;
            indicators[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPager.setCurrentItem(finalI1);
                }
            });
        }
        //指示器与viewPager联动
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i=0;i<imageViews.size();i++){
                    if (i==position){
                        indicators[i].setBackgroundResource(R.drawable.white);
                    }else{
                        indicators[i].setBackgroundResource(R.drawable.gray);
                    }
                }
                if (position==imageViews.size()-1){
                    TV_start.setVisibility(View.VISIBLE);
                    TV_setPort.setVisibility(View.VISIBLE);
                }else{
                    TV_setPort.setVisibility(View.GONE);
                    TV_start.setVisibility(View.GONE);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //设置适配器
        viewPager.setAdapter(new viewPagerAdapter(imageViews));
    }
    //设置IP端口
    public void setPortPath(View view){
        Intent intent = new Intent(MainActivity.this,SetPort.class);
        startActivity(intent);
    }
    public void startSofter(View view){
        Intent intent = new Intent(MainActivity.this,SetPort.class);
        startActivity(intent);
    }
}